﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CodeGenerator.Interface;
using CodeGenerator.Control;

namespace CodeGenerator.View
{
    public partial class GIMainForm : Form
    {
        #region
        //声明全局的数据库连接对象
        private static SqlConnection conn = null;
        //创建一个获取数据库连接对象类的实例
        private IConn connSqlServer = null;
        //创建一个获取数据库信息类的实例
        private IGetDatabaseInfo getDatabaseInfo = null;
        //被选中的字段
        private string field = null;
        //消息框内容
        private string msg = string.Empty;
        /// <summary>
        /// 指定表的所有字段及其数据类型
        /// </summary>
       // private static DataTable dt = new DataTable();
        /// <summary>
        /// 存储已选的字段及其数据类型
        /// </summary>
        private static DataTable dtSelected = new DataTable();
        /// <summary>
        /// 用于生成类文件
        /// </summary>
        private CGenerateClass generateClass = new CGenerateClass();
        #endregion

        public GIMainForm()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            connSqlServer = new CConnSqlServer2005();
            getDatabaseInfo = new CGetDatabaseInfoSqlServer2005();

            //数据库连接信息
            string host = tbDBAddress.Text.Trim();
            string database = tbDBName.Text.Trim();
            string userID = tbUser.Text.Trim();
            string password = tbPWD.Text.Trim();
            string connStr = "Data Source='" + host + "';;Initial Catalog='" + database + "';Persist Security Info=True;User ID='" + userID + "';Password='" + password + "';";

            if (conn==null)
            {
                //并创建数据库连接对象
                conn = new SqlConnection(connStr);
            }
            //声明一个内存表存储指定数据库的所有表名
            DataTable dt = new DataTable();
            try
            {
                //获取指定数据库所有表名        
                dt = getDatabaseInfo.GetTableName(database, conn);
                //禁用再次连接
                btnConnect.Enabled = false;

                //打开功能
                groupBox2.Enabled = true;
                groupBox3.Enabled = true;

                //绑定表名
                BindingTableName(dt);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 生成表名的ListBox
        /// </summary>
        /// <param name="dt">数据表信息</param>
        private void BindingTableName(DataTable dt)
        {
            lbTable.Items.Clear();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lbTable.Items.Add(dt.Rows[i][2].ToString());
            }

        }

        private void GIMainForm_Load(object sender, EventArgs e)
        {
            Binding();
        }

        /// <summary>
        /// Form加载的事件
        /// </summary>
        private void Binding()
        {
            //
        }

        private void lbTable_Click(object sender, EventArgs e)
        {
            string database = tbDBName.Text.ToString();//数据库名
            string tablename  = lbTable.SelectedItem.ToString(); //当前选中的表名

            tbEntity.Text = tablename;//生成类的实体名

            DataTable fieldDT = getDatabaseInfo.GetField(database, tablename, conn);
            dgViewTable.DataSource = fieldDT;
            //dgViewTable.Columns["CSharpType"].DataGridView.DataSource = new string[]{"aaa", "bbb", "ccc"};

            //CSharpType.DataPropertyName = "type";此处最近加入一个combobox的显示列表

            //dtselect赋值
            dtSelected = fieldDT;
        }

        /// <summary>
        /// 显示文件对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnChooseFileUrl_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbDialog = new FolderBrowserDialog();
            fbDialog.ShowDialog();

            tbFileUrl.Text = fbDialog.SelectedPath;
        }

        /// <summary>
        /// 生成单表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerTable_Click(object sender, EventArgs e)
        {
            generateClass.BuildAllClass(lbTable.SelectedItem.ToString(), dtSelected, tbEntity.Text.ToString(), tbNamespace.Text.ToString(), tbFileUrl.Text.ToString());
            MessageBox.Show("生成代码完成");
        }



    }
}
